// RMSTranslator.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "RMSTranslator.h"
#include "RMSTranslatorDlg.h"
#include "RegistrationDlg.h"
#include "..\Common\XMessageBox.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CRMSTranslatorApp

BEGIN_MESSAGE_MAP(CRMSTranslatorApp, CWinApp)
	//{{AFX_MSG_MAP(CRMSTranslatorApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CRMSTranslatorApp construction

CRMSTranslatorApp::CRMSTranslatorApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CRMSTranslatorApp object

CRMSTranslatorApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CRMSTranslatorApp initialization
#define MAX_DAYS	7
//#define MAX_DAYS	30

BOOL CRMSTranslatorApp::InitInstance()
{
//#define EXPIRED 
#ifdef _OEM_
	#ifndef _TEC_ // no expiration for TEC !
		#define EXPIRED
	#endif
#endif

#ifdef EXPIRED
	SetRegistryKey("Streambox");

    int RunTime;
    int DaysLeft=MAX_DAYS;

    RunTime = GetProfileInt("","run0",-1);
    DaysLeft = GetProfileInt("","run1",-1);

    bool bExpired=false;
    int day;
    
    if (RunTime!=-1) {
        if (abs(time(0)-RunTime) > 24*3600) {
            DaysLeft -= abs(time(0)-RunTime) / (24*3600);
            WriteProfileInt("","run0",time(0));
            WriteProfileInt("","run1",DaysLeft);
        }
        
        if (DaysLeft<=0)
            bExpired=true;
    }
    else {
        WriteProfileInt("","run0",time(0));
        WriteProfileInt("","run1",MAX_DAYS);
        DaysLeft=MAX_DAYS;
    }


    if (bExpired) {
         MessageBox(NULL, "The application has expired. Please contact \nsupport@streambox.com for more information.", "VC-10 Client", MB_OK | MB_ICONWARNING);
         return FALSE;
    }
    else {
        char szTrialMessage[100];
        sprintf(szTrialMessage,"You are on day %d of your %d-day trial period\n\nOnly %d day(s) left to buy a fullversion", MAX_DAYS+1-DaysLeft, MAX_DAYS, DaysLeft);
               
        
        if (XMessageBox(NULL,szTrialMessage,"Trial Reminder",MB_STARTBUY | MB_NORESOURCE,0)==IDBUY) {
            ShellExecute(0,"open","http://www.streambox.com","","",SW_SHOW);
            return FALSE;            
        }    
    }	
#else
	// Registration key
	char szName[256];
	char szKey[KEY_LENGTH+1];

	bool bRegistration = true;
	if(REGISTRY_LoadString(KEY_REGISTRATION_NAME, szName, 256))
	{
		if(REGISTRY_LoadString(KEY_REGISTRATION_KEY, szKey, KEY_LENGTH+1))
		{
			if(CheckKey(szName, szKey))
			{
				bRegistration = false;
			}
		}
	}
	if(bRegistration)
	{
		CRegistrationDlg dlg;
		int count = 0;
		while(count < 5)
		{
			if(dlg.DoModal() == IDOK)
			{
				if(CheckKey(static_cast<LPCTSTR>(dlg.m_strName), static_cast<LPCTSTR>(dlg.m_strKey)))
				{
					bRegistration = false;
					REGISTRY_SaveString(KEY_REGISTRATION_NAME, static_cast<LPCTSTR>(dlg.m_strName));
					REGISTRY_SaveString(KEY_REGISTRATION_KEY, static_cast<LPCTSTR>(dlg.m_strKey));
					break;
				}
				else
				{
					MessageBox(NULL, "Registration key is not valid.","RMS Client", MB_OK | MB_ICONWARNING);
				}
			}
			else
			{
				return FALSE;
			}
			count++;
		}
	}

	if(bRegistration) return FALSE;

#endif

	WORD wVersionRequested;
	WSADATA wsaData;
 
	wVersionRequested = MAKEWORD( 2, 0 );
 
	WSAStartup( wVersionRequested, &wsaData );

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CRMSTranslatorDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	WSACleanup();
	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}
